var KOC = require("koc.common.utils");

var CommonUtils = require("../../lib/common-utils");
var DALFactory = require("../../lib/dal/dal-factory");

var Interface_Geetest = require("../../lib/interface-geetest");
var Interface_QCloud_SMS = require("../../lib/interface-qcloud-sms");

module.exports = {
  Service: function (req, res) {
    switch (req.params.type) {
      //region Base
      case "base.phoneverifycode":
        ajaxFactory.Base.PhoneVerifyCode(req, res);
        return;
      case "base.geetestregister":
        ajaxFactory.Base.GeetestRegister(req, res);
        return;
      //endregion
      //region DataBind
      case "databind.userbanktransaction":
        ajaxFactory.DataBind.UserBankTransaction(req, res);
        return;
      case "databind.userpackagetransaction":
        ajaxFactory.DataBind.UserPackageTransaction(req, res);
        return;
      case "databind.familybanktransaction":
        ajaxFactory.DataBind.FamilyBankTransaction(req, res);
        return;
      case "databind.commoditygift":
        ajaxFactory.DataBind.CommodityGift(req, res);
        return;
      case "databind.commodityguardian":
        ajaxFactory.DataBind.CommodityGuardian(req, res);
        return;
      case "databind.commodityhorn":
        ajaxFactory.DataBind.CommodityHorn(req, res);
        return;
      case "databind.commoditymounts":
        ajaxFactory.DataBind.CommodityMounts(req, res);
        return;
      case "databind.commodityvip":
        ajaxFactory.DataBind.CommodityVIP(req, res);
        return;
      //endregion
    }
    res.jsonp({flag: -9999, msg: "未知的请求"});
  }
};

var ajaxFactory = {
  Base: {
    //region PhoneVerifyCode:手机验证码
    PhoneVerifyCode: function (req, res) {
      var _parm = {
        Phone: req.params.Phone
      };
      if (!(/^1\d{10}$/).test(_parm.Phone)) {
        res.jsonp({flag: 2, msg: "请输入正确的手机号"});
        return;
      }
      KOC.Async.series([
        //极验验证
        // function (callback) {
        //   Interface_Geetest.Validate({
        //     challenge: req.params.geetest_challenge,
        //     validate: req.params.geetest_validate,
        //     seccode: req.params.geetest_seccode
        //   }, function (retValue) {
        //     if (retValue.hasError) {
        //       callback(retValue);
        //       return;
        //     }
        //     callback();
        //   });
        // },
        //获取验证码
        function (callback) {
          DALFactory.RedisCommon.PhoneAuthCode.Get(_parm.Phone, function (retValue) {
            if (retValue.hasError) {
              callback(retValue);
              return;
            }
            _parm.Code = retValue.returnObject.Code;
            callback();
          });
        },
        //发送验证码短信
        function (callback) {
          Interface_QCloud_SMS.SendSMS_VerifyCode(_parm.Phone, _parm.Code, function (retValue) {
            if (retValue.hasError) {
              callback(retValue);
              return;
            }
            callback();
          });
        }
      ], function (retValue) {
        if (retValue) {
          res.jsonp({flag: retValue.errorCode, msg: retValue.message});
          return;
        }
        res.jsonp({flag: 0, msg: "验证码已发送"});
      });
    },
    //endregion
    //region GeetestRegister:验证码服务(极验)
    GeetestRegister: function (req, res) {
      Interface_Geetest.Register(function (retValue) {
        if (retValue.hasError) {
          res.jsonp({flag: 1, msg: "请求验证码出错，请重试"});
          return;
        }
        res.jsonp({
          flag: 0,
          msg: "",
          data: {
            ID: retValue.returnObject.ID,
            Challenge: retValue.returnObject.Challenge
          }
        });
      });
    }
    //endregion
  },
  DataBind: {
    //region BankTransaction:个人帐户交易类型
    UserBankTransaction: function (req, res) {
      res.jsonp({
        flag: 0,
        data: CommonUtils.User.BankTransaction.Data
      });
    },
    //endregion
    //region UserPackageTransaction:用户包裹交易类型
    UserPackageTransaction: function (req, res) {
      res.jsonp({
        flag: 0,
        data: CommonUtils.User.PackageTransaction.Data
      });
    },
    //endregion
    //region BankTransaction:公会帐户交易类型
    FamilyBankTransaction: function (req, res) {
      res.jsonp({
        flag: 0,
        data: CommonUtils.Family.BankTransaction.Data
      });
    },
    //endregion
    //region CommodityGift:礼物商品
    CommodityGift: function (req, res) {
      var _Data = KOC.Lodash.sortBy(CommonUtils.Commodity.Gift.Data, function (ThisValue) {
        return KOC.StringUtils.ToIntPositive(ThisValue.Price);
      });
      var _DataList = _Data;
      _Data = [];
      KOC.Lodash.forEach(_DataList, function (ThisValue) {
        ThisValue = KOC.Lodash.pick(ThisValue,
          [
            "id"
            , "Name"
            , "Intro"
            , "Price"
            , "EXP"
            , "EXP_Anchor"
            , "UserLevel"
            , "PurchaseMode"
            , "IsVIP"
            , "IsGuardian"
            , "IsLucky"
            , "IsActivity"
            , "IsTask"
            , "IsHeadline"
            , "IsRunway"
            , "IsFloat"
            , "IsPond"
            , "IsWeekStar"
            , "Tag"
            , "Status"
          ]);
        ThisValue.EXP = KOC.StringUtils.ToIntPositive(ThisValue.EXP);
        ThisValue.EXP_Anchor = KOC.StringUtils.ToIntPositive(ThisValue.EXP_Anchor);
        ThisValue.UserLevel = KOC.StringUtils.ToIntPositive(ThisValue.UserLevel);
        ThisValue.PurchaseMode = KOC.StringUtils.ToIntPositive(ThisValue.PurchaseMode);
        ThisValue.IsVIP = KOC.StringUtils.ToBoolean(ThisValue.IsVIP);
        ThisValue.IsGuardian = KOC.StringUtils.ToBoolean(ThisValue.IsGuardian);
        ThisValue.IsLucky = KOC.StringUtils.ToBoolean(ThisValue.IsLucky);
        ThisValue.IsActivity = KOC.StringUtils.ToBoolean(ThisValue.IsActivity);
        ThisValue.IsTask = KOC.StringUtils.ToBoolean(ThisValue.IsTask);
        ThisValue.IsHeadline = KOC.StringUtils.ToBoolean(ThisValue.IsHeadline);
        ThisValue.IsRunway = KOC.StringUtils.ToBoolean(ThisValue.IsRunway);
        ThisValue.IsFloat = KOC.StringUtils.ToBoolean(ThisValue.IsFloat);
        ThisValue.IsPond = KOC.StringUtils.ToBoolean(ThisValue.IsPond);
        ThisValue.IsWeekStar = KOC.StringUtils.ToBoolean(ThisValue.IsWeekStar);
        ThisValue.Status = KOC.StringUtils.ToIntPositive(ThisValue.Status);
        _Data.push(ThisValue);
      });
      res.jsonp({
        flag: 0,
        data: _Data
      });
    },
    //endregion
    //region CommodityGuardian:守护商品
    CommodityGuardian: function (req, res) {
      var _Data = KOC.Lodash.sortBy(CommonUtils.Commodity.Guardian.Data, function (ThisValue) {
        return KOC.StringUtils.ToIntPositive(ThisValue.Price);
      });
      var _DataList = _Data;
      _Data = [];
      KOC.Lodash.forEach(_DataList, function (ThisValue) {
        ThisValue = KOC.Lodash.pick(ThisValue,
          [
            "id"
            , "Name"
            , "Price"
            , "Mode"
            , "Value"
            , "EXP"
            , "EXP_Anchor"
            , "Status"
          ]);
        ThisValue.Value = KOC.StringUtils.ToIntPositive(ThisValue.Value);
        ThisValue.EXP = KOC.StringUtils.ToIntPositive(ThisValue.EXP);
        ThisValue.EXP_Anchor = KOC.StringUtils.ToIntPositive(ThisValue.EXP_Anchor);
        ThisValue.Status = KOC.StringUtils.ToIntPositive(ThisValue.Status);
        _Data.push(ThisValue);
      });
      res.jsonp({
        flag: 0,
        data: _Data
      });
    },
    //endregion
    //region CommodityHorn:喇叭商品
    CommodityHorn: function (req, res) {
      var _Data = KOC.Lodash.sortBy(CommonUtils.Commodity.Horn.Data, function (ThisValue) {
        return KOC.StringUtils.ToIntPositive(ThisValue.Price);
      });
      var _DataList = _Data;
      _Data = [];
      KOC.Lodash.forEach(_DataList, function (ThisValue) {
        ThisValue = KOC.Lodash.pick(ThisValue,
          [
            "Mode"
            , "Name"
            , "Price"
            , "Value"
            , "EXP"
            , "EXP_Anchor"
            , "Status"
          ]);
        ThisValue.id = ThisValue.Mode;
        ThisValue.Value = KOC.StringUtils.ToIntPositive(ThisValue.Value);
        ThisValue.EXP = KOC.StringUtils.ToIntPositive(ThisValue.EXP);
        ThisValue.EXP_Anchor = KOC.StringUtils.ToIntPositive(ThisValue.EXP_Anchor);
        ThisValue.Status = KOC.StringUtils.ToIntPositive(ThisValue.Status);
        _Data.push(ThisValue);
      });
      res.jsonp({
        flag: 0,
        data: _Data
      });
    },
    //endregion
    //region CommodityMounts:座骑商品
    CommodityMounts: function (req, res) {
      var _Data = KOC.Lodash.sortBy(CommonUtils.Commodity.Mounts.Data, function (ThisValue) {
        return KOC.StringUtils.ToIntPositive(ThisValue.Price);
      });
      var _DataList = _Data;
      _Data = [];
      KOC.Lodash.forEach(_DataList, function (ThisValue) {
        ThisValue = KOC.Lodash.pick(ThisValue,
          [
            "id"
            , "Name"
            , "Price"
            , "Mode"
            , "Value"
            , "EXP"
            , "UserLevel"
            , "Mall"
            , "Status"
          ]);
        ThisValue.Value = KOC.StringUtils.ToIntPositive(ThisValue.Value);
        ThisValue.EXP = KOC.StringUtils.ToIntPositive(ThisValue.EXP);
        ThisValue.UserLevel = KOC.StringUtils.ToIntPositive(ThisValue.UserLevel);
        ThisValue.Mall = KOC.StringUtils.ToBoolean(ThisValue.Mall, true);
        ThisValue.Status = KOC.StringUtils.ToIntPositive(ThisValue.Status);
        _Data.push(ThisValue);
      });
      res.jsonp({
        flag: 0,
        data: _Data
      });
    },
    //endregion
    //region CommodityVIP:VIP商品
    CommodityVIP: function (req, res) {
      var _Data = KOC.Lodash.sortBy(CommonUtils.Commodity.VIP.Data, function (ThisValue) {
        return KOC.StringUtils.ToIntPositive(ThisValue.Price);
      });
      var _DataList = _Data;
      _Data = [];
      KOC.Lodash.forEach(_DataList, function (ThisValue) {
        ThisValue = KOC.Lodash.pick(ThisValue,
          [
            "id"
            , "Name"
            , "Price"
            , "Mode"
            , "Value"
            , "EXP"
            , "Status"
          ]);
        ThisValue.Value = KOC.StringUtils.ToIntPositive(ThisValue.Value);
        ThisValue.EXP = KOC.StringUtils.ToIntPositive(ThisValue.EXP);
        ThisValue.Status = KOC.StringUtils.ToIntPositive(ThisValue.Status);
        _Data.push(ThisValue);
      });
      res.jsonp({
        flag: 0,
        data: _Data
      });
    }
    //endregion
  }
};